<?php
function deleteMembre($id){
    global $bdd;

    $req = $bdd->prepare('DELETE FROM membres WHERE ID = ?');
    $req->execute(array($id)) or die(print_r($req->errorInfo()));
    return '';
}

function getMembres($start, $nombre){
    global $bdd;

    $req = $bdd->query('SELECT ID, pseudo, mail, nom, prenom FROM membres ORDER BY ID LIMIT '.$start.','.$nombre) or die(print_r($bdd->errorInfo()));
    $result = $req->fetchAll();
    return $result;
}

function getNbMembres(){
    global $bdd;

    $req = $bdd->query('SELECT COUNT(*) AS nbMembres FROM `membres`') or die(print_r($bdd->errorInfo()));
    $result = $req->fetch();
    $return = $result['nbMembres'];
    $req->closeCursor();
    return $return;
}

function updateInfosPerso($id, $nom, $prenom, $mail, $adresse, $codepostal, $ville, $pays){
    global $bdd;

    $req = $bdd->prepare('UPDATE membres SET nom = :nom, prenom = :prenom, mail = :mail, adresse = :adresse, codepostal = :codepostal, ville = :ville, pays = :pays WHERE ID = :id');
    $req->execute(array('nom' => $nom, 'prenom' => $prenom, 'mail' => $mail, 'adresse' => $adresse, 'codepostal' => $codepostal,
        'ville' => $ville, 'pays' => $pays, 'id' => $id)) or die(print_r($req->errorInfo()));
    $req->closeCursor(); return '';
}
function updateMembre($id, $nom, $prenom, $mail, $adresse, $codepostal, $ville, $pays, $admin){
    global $bdd;

    $req = $bdd->prepare('UPDATE membres SET nom = :nom, prenom = :prenom, mail = :mail, adresse = :adresse, codepostal = :codepostal, ville = :ville, pays = :pays, admin = :admin WHERE ID = :id');
    $req->execute(array('nom' => $nom, 'prenom' => $prenom, 'mail' => $mail, 'adresse' => $adresse, 'codepostal' => $codepostal,
        'ville' => $ville, 'pays' => $pays, 'id' => $id, 'admin' => $admin)) or die(print_r($req->errorInfo()));
    $req->closeCursor(); return '';
}

function updatePassword($id, $password){
    global $bdd;
    
    $req = $bdd->prepare('UPDATE membres SET password = :password WHERE ID = :id');
    $req->execute(array('password' => $password, 'id' => $id)) or die(print_r($req->errorInfo()));
    $req->closeCursor();
    $result = $req->fetchAll();
    return $result;
}

function inscrireMembre($pseudo, $password, $mail){
    global $bdd;

    $req = $bdd->prepare('INSERT INTO membres(pseudo, password, mail, date_inscription) VALUES(:pseudo, :password, :mail, CURRENT_DATE())');
    $req->execute(array('pseudo' => $pseudo, 'password' => $password, 'mail' => $mail)) or die(print_r($req->errorInfo()));
    $req->closeCursor();
}

function verifPseudoUsed($pseudo){
    global $bdd;

    $req = $bdd->prepare('SELECT pseudo FROM membres WHERE pseudo = :pseudo');
    $req->execute(array('pseudo' => $pseudo)) or die(print_r($req->errorInfo()));
    $result = $req->fetchAll();
    return $result;
}

function selectIDmembre($pseudo, $password){
    global $bdd;
    
    $req = $bdd->prepare('SELECT ID, pseudo FROM membres WHERE pseudo = :pseudo AND password = :password LIMIT 0,1');
    $req->execute(array('pseudo' => $pseudo, 'password' => $password)) or die(print_r($req->errorInfo()));
    $resultat = $req->fetchAll();
    return $resultat;

}

function selectInfosMembre($id){
    global $bdd;

    $req = $bdd->prepare('SELECT * FROM membres WHERE ID = ? LIMIT 0,1');
    $req->execute(array($id)) or die(print_r($req->errorInfo()));
    $result = $req->fetchAll();
    return $result;
}